package com.car.loan.project.xr.controller;

import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanAlbum;
import com.car.loan.project.xr.mapper.LoanAlbumMapper;
import com.car.loan.project.xr.service.ILoanAlbumService;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.util.bo.business.LoanAlbumBo;
import com.car.loan.project.xr.util.bo.business.modify.LoanModifyBo;
import com.car.loan.project.xr.util.bo.business.modify.LoanModifyPageParam;
import com.car.loan.project.xr.util.enums.LoanAlbumType;
import com.car.loan.project.xr.util.vo.LoanAlbumVo;
import com.car.loan.project.xr.util.vo.modify.LoanModifyDetail;
import com.car.loan.project.xr.util.vo.modify.LoanModifyVo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @date 2020/2/18 22:08
 */
@RestController
@RequestMapping("xr/loan/modify")
@Api(value = "业务修改", tags = "车贷业务-业务修改")
public class LoanModifyController {


    @Autowired
    private LoanAlbumMapper loanAlbumMapper;


    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanAlbumService loanAlbumService;

    @PreAuthorize("@ss.hasPermi('xr:loan:modify')")
    @GetMapping("list")
    @ApiOperation(value = "获取业务修改列表", response = LoanModifyVo.class, responseContainer = "list")
    public AjaxResult modifyList(@Valid LoanModifyPageParam loanModifyPageParam) {
        boolean isAdminId =
            null != loanModifyPageParam.getAdminId() && loanModifyPageParam.getAdminId() > 0;
        boolean isBankId =
            null != loanModifyPageParam.getBankId() && loanModifyPageParam.getBankId() > 0;
        boolean isDeptId =
            null != loanModifyPageParam.getDeptId() && loanModifyPageParam.getDeptId() > 0;
        boolean isDealerId =
            null != loanModifyPageParam.getDealerId() && loanModifyPageParam.getDealerId() > 0;
        boolean isName = StringUtils.isNotEmpty(loanModifyPageParam.getCustomerName());
        if (isAdminId || isBankId || isDealerId || isDeptId || isName) {
            return AjaxResult.success(loanService.modifyPage(loanModifyPageParam));
        }
        return AjaxResult.success(Lists.newArrayList());
    }

    @PreAuthorize("@ss.hasPermi('xr:loan:modify')")
    @GetMapping("detail/{loanId}")
    @ApiOperation(value = "获取业务修改详情", response = LoanModifyDetail.class)
    public AjaxResult modifyList(@ApiParam("业务id") @PathVariable("loanId") Long loanId) {
        return AjaxResult.success(loanService.getModifyDetail(loanId));
    }


    @PostMapping("business")
    @ApiOperation("业务修改")
    @PreAuthorize("@ss.hasPermi('xr:loan:modify')")
    @Log(title = "业务信息修改", businessType = BusinessType.UPDATE)
    public AjaxResult businessModify(@RequestBody @Valid LoanModifyBo loanModifyBo) {
        return loanService.modifyBusiness(loanModifyBo);
    }


    @PreAuthorize("@ss.hasPermi('xr:loan:album')")
    @PutMapping("album")
    @ApiOperation("附件添加")
    @Log(title = "业务修改-附件添加", businessType = BusinessType.INSERT)
    public synchronized AjaxResult putImages(
        @Valid @RequestBody LoanDataAuditController.LoanAlbums loanAlbumVo) {
        List<LoanAlbumVo> loanAlbumVos = loanAlbumVo.getLoanAlbumVos();
        if (null != loanAlbumVos && loanAlbumVos.size() > 0) {
            for (LoanAlbumVo albumVo : loanAlbumVos) {
                LoanAlbum loanAlbum = new LoanAlbum();
                loanAlbum.setLoanId(albumVo.getLoanId());
                loanAlbum.setType(albumVo.getType());
                loanAlbum.setUrl(albumVo.getDbUrl());
                loanAlbumService.insertLoanAlbum(loanAlbum);
            }
        }
        return AjaxResult.success();
    }


    @DeleteMapping("album/{id}")
    @ApiOperation("附件添加")
    @PreAuthorize("@ss.hasPermi('xr:loan:album')")
    @Log(title = "业务修改-附件删除", businessType = BusinessType.DELETE)
    public AjaxResult albumDeleteModify(@ApiParam("图片id") @PathVariable("id") Long id) {
        loanAlbumMapper.deleteById(id);
        return AjaxResult.success();
    }

}
